//var yeas = new Date().getFullYear();
//var munth = new Date().getMonth()+1;
var yeas = new Date().getFullYear();
var munth = new Date().getMonth()+1;
document.querySelector("#q").innerHTML = yeas;
document.querySelector("#za").innerHTML = munth;
var day = 0;


function runNian(y){
    if (y % 400 === 0 || (y % 4 === 0 && y % 100 !== 0)){
        return true;
    }else{
        return false;
    }
}

for (i = 1980; i< yeas; i++){
    if (runNian(i)){
        day += 366;
    }else{
        day +=365;
    }
}
//2019年 1 ~ 4 月天数
var day1 = 0;
for (t = 1;t < munth; t++){
    switch (t) {
        case 1:
            day1 += 31;
            break;
        case 2:
            if (runNian(yeas)){
                 day1 += 29;
            }else{
                day1 += 28;
            }
            break;
        case 3:
            day1 += 31;
            break;
        case 4:
            day1 += 30;
            break;
        case 5:
            day1 += 31;
            break;
        case 6:
            day1 += 30;
            break;
        case 7:
            day1 += 31;
            break;
        case 8:
            day1 += 31;
            break;
        case 9:
            day1 += 30;
            break;
        case 10:
            day1 += 31;
            break;
        case 11:
            day1 += 30;
            break;
        case 12:
            day1 += 31;
            break;
    }
}




//本月天数
var day2 = 0;
switch (munth){
    case 1:
        day2 += 31;
        break;
    case 2:
        if (runNian(yeas)){
            day2 += 29;
        }else{
            day2 += 28;
        }
        break;
    case 3:
        day2 += 31;
        break;
    case 4:
        day2 += 30;
        break;
    case 5:
        day2 += 31;
        break;
    case 6:
        day2 += 30;
        break;
    case 7:
        day2 += 31;
        break;
    case 8:
        day2 += 31;
        break;
    case 9:
        day2 += 30;
        break;
    case 10:
        day2 += 31;
        break;
    case 11:
        day2 += 30;
        break;
    case 12:
        day2 += 31;
        break;
}
//上个月天数
var day3 = 0;
if(munth == 1){
    day3 += 31
}else {
    switch (munth-1){
        case 1:
            day3 += 31;
            break;
        case 2:
            if (runNian(yeas)){
                day3 += 29;
            }else{
                day3 += 28;
            }
            break;
        case 3:
            day3 += 31;
            break;
        case 4:
            day3 += 30;
            break;
        case 5:
            day3 += 31;
            break;
        case 6:
            day3 += 30;
            break;
        case 7:
            day3 += 31;
            break;
        case 8:
            day3 += 31;
            break;
        case 9:
            day3 += 30;
            break;
        case 10:
            day3 += 31;
            break;
        case 11:
            day3 += 30;
            break;
        case 12:
            day3 += 31;
            break;
    }
}

day += day1;

day += 2;

//日历一共可以放多少天
var sum = 42;
//本月一号是星期几
var stary = day % 7;
//下个月一共有几天
var nextDays = sum -stary - day2;

var thisday = new Date().getDate();

var date1 = document.querySelector("#date1");

if(day3 -stary +1 > day3){
    for(var a= day3 -7 +1; a <= day3; a++){
        date1.innerHTML += "<span class='h'>"+ a +"</span>";
    }
}else {
    for(var j = day3 -stary +1; j <= day3; j++){
        date1.innerHTML += "<span class='h'>"+ j +"</span>";
    }
}
for ( w = 1; w <= day2; w++){
    if (w == thisday){
        date1.innerHTML += "<span class='today'>" + w + "</span>";
    }else{
        date1.innerHTML += "<span>" + w + "</span>";
    }
}
if(stary == 0){
    for (var x = 1; x <= sum -7 - day2; x++){
        date1.innerHTML += "<span class='h'>"+ x +"</span>";
    }
}else {
    for (var s = 1; s <= nextDays; s++){
        date1.innerHTML += "<span class='h'>"+ s +"</span>";
    }
}
